package com.xuelang.pipeline.app.resource.impl;

import cn.hutool.core.util.IdUtil;
import com.xuelang.hiforce.base.tracing.TracingUtils;
import com.xuelang.jadp.client.sys.dto.UserInfoDTO;
import com.xuelang.pipeline.app.resource.ToolChainService;
import com.xuelang.pipeline.app.resource.converter.ToolChainDTOToToolChainConverter;
import com.xuelang.pipeline.client.common.constant.Constant;
import com.xuelang.pipeline.client.common.exception.BizException;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.common.util.BizAssert;
import com.xuelang.pipeline.client.resource.dto.ToolChainDTO;
import com.xuelang.pipeline.client.resource.dto.ToolChainQuery;
import com.xuelang.pipeline.client.resource.enums.ToolChainTypeEnum;
import com.xuelang.pipeline.domain.resource.ToolChain;
import com.xuelang.pipeline.domain.resource.service.ToolChainDomainService;
import com.xuelang.sys.app.sys.UserManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 工具链服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class ToolChainServiceImpl implements ToolChainService {

    @Autowired
    ToolChainDomainService toolChainDomainService;
    @Autowired
    UserManagerService userManagerService;

    @Override
    public PageRecord<ToolChainDTO> pageList(PageParam<ToolChainQuery> dtoPageParam) {
        ToolChainQuery condition = dtoPageParam.getCondition();
        condition = condition == null ? new ToolChainQuery() : condition;
        PageRecord<ToolChain> pageRecord = toolChainDomainService.pageList(
                dtoPageParam.getCurrentPage(),
                dtoPageParam.getPageSize(),
                condition);
        List<ToolChainDTO> record = pageRecord.getRecords()
                .stream()
                .map(ToolChainDTOToToolChainConverter.INSTANCE::toolChainToToolChainDTO)
                .collect(Collectors.toList());
        List<ToolChainDTO> fullfilledCreator = record.stream().map(this::fullfillCreator).collect(Collectors.toList());

        return new PageRecord<>(
                pageRecord.getCurrentPage(),
                pageRecord.getPageSize(),
                pageRecord.getTotalCount(),
                pageRecord.getTotalPages(),
                fullfilledCreator);
    }

    private ToolChainDTO fullfillCreator(ToolChainDTO toolChainDTO){
        String creatorId = toolChainDTO.getCreatorId();
        UserInfoDTO user = userManagerService.getUserById(creatorId);
        if (Objects.nonNull(user)) {
            toolChainDTO.setCreator(user.getRealName());
        }else{
            toolChainDTO.setCreator(Constant.UNKNOWN_USER);
        }
        return toolChainDTO;
    }

    @Override
    public List<ToolChainDTO> list(ToolChainQuery toolChainQuery) {
        return toolChainDomainService.list(toolChainQuery).stream().map(ToolChainDTOToToolChainConverter.INSTANCE::toolChainToToolChainDTO).collect(Collectors.toList());
    }


    @Override
    public Boolean add(ToolChainDTO toolChainDTO) {
        BizAssert.notNull(toolChainDTO.getType(), "type不能为空");
        BizAssert.notEmpty(toolChainDTO.getVersion(), "version不能为空");
        BizAssert.notEmpty(toolChainDTO.getServer(), "server不能为空");
        if (!Objects.equals(toolChainDTO.getType(), ToolChainTypeEnum.TYPE_CI.getTypeKey())
                && !Objects.equals(toolChainDTO.getType(), ToolChainTypeEnum.TYPE_CD.getTypeKey())
                && !Objects.equals(toolChainDTO.getType(), ToolChainTypeEnum.TYPE_QA.getTypeKey())) {
            throw new BizException("type参数错误.取值为：1持续集成工具2持续部署工具3代码质量检查工具");
        }
        ToolChain toolChain = ToolChainDTOToToolChainConverter.INSTANCE.toolChainDTOToToolChain(toolChainDTO);
        toolChain.setCode(IdUtil.fastSimpleUUID());
        toolChain.setCreatorId(TracingUtils.getUserId());
        toolChain.setUpdaterId(TracingUtils.getUserId());
        return toolChainDomainService.save(toolChain);
    }

    @Override
    public Boolean update(ToolChainDTO toolChainDTO) {
        BizAssert.notEmpty(toolChainDTO.getCode(), "编码不能为空");
        BizAssert.notNull(toolChainDTO.getType(), "类型不能为空");
        BizAssert.notEmpty(toolChainDTO.getVersion(), "版本不能为空");
        BizAssert.notEmpty(toolChainDTO.getServer(), "服务地址不能为空");
        if (!Objects.equals(toolChainDTO.getType(), ToolChainTypeEnum.TYPE_CI.getTypeKey())
                && !Objects.equals(toolChainDTO.getType(), ToolChainTypeEnum.TYPE_CD.getTypeKey())
                && !Objects.equals(toolChainDTO.getType(), ToolChainTypeEnum.TYPE_QA.getTypeKey())) {
            throw new BizException("type参数错误.取值为：1持续集成工具2持续部署工具3代码质量检查工具");
        }
        ToolChain toolChain = ToolChainDTOToToolChainConverter.INSTANCE.toolChainDTOToToolChain(toolChainDTO);
        toolChain.setCreatorId(TracingUtils.getUserId());
        toolChain.setUpdaterId(TracingUtils.getUserId());
        return toolChainDomainService.update(toolChain);
    }

    @Override
    public Boolean delete(String code) {
        BizAssert.notEmpty(code, "编码不能为空");
        return toolChainDomainService.delete(code);
    }

    @Override
    public ToolChainDTO query(String code) {
        BizAssert.notEmpty(code, "编码不能为空");
        return ToolChainDTOToToolChainConverter.INSTANCE.toolChainToToolChainDTO(toolChainDomainService.query(code));
    }
}
